package hteink.api;

import android.util.Log;

import androidx.annotation.NonNull;

import java.lang.reflect.Constructor;
import java.util.concurrent.atomic.AtomicReference;

import hteink.api.impl.DpiManagerImplDummy;
import hteink.api.intf.IDpiManager;

public class EinkFactoryHelper {
    private static final String TAG = EinkFactoryHelper.class.getSimpleName();

    private static final AtomicReference<IDpiManager> sCachedDpiManagerRef = new AtomicReference<>();

    public static IDpiManager getDpiManager() {
        IDpiManager manager = sCachedDpiManagerRef.get();
        if (manager != null) {
            return manager;
        }
        manager = createDpiManager();
        sCachedDpiManagerRef.set(manager);
        return manager;
    }

    @NonNull
    private static IDpiManager createDpiManager() {
        try {
            Class<?> optClass = Class.forName("hteink.api.impl.DpiManagerImpl");
            Constructor<?> constructor = optClass.getConstructor();
            return (IDpiManager) constructor.newInstance();
        } catch (Exception e) {
            Log.e(TAG, "createDpiManager: failed with Exception!!", e);
            e.printStackTrace();
        }
        return new DpiManagerImplDummy();
    }
}
